<?php

load_helper('html');
assets::instance() ->css( "list" );

$header = '';
foreach( $_header as $row )
{
	$header .= '<tr>';
	foreach( $row as $cell )
	{
		$th = array( );
		foreach( array( 'class', 'title', 'width' ) as $k )
			if( isset( $cell[$k] ) )
				$th[$k] = $cell[$k];
		
		if (isset($th['title']))
			$th['title'] = $th['title'];
		
		if( @$cell['columns'] > 1 )
			$th['colspan'] = $cell['columns'];
		if( @$cell['rows'] > 1 )
			$th['rowspan'] = $cell['rows'];

		$th['html'] = isset($cell['html']) ? $cell['html'] : (isset($cell['text']) ? $cell['text'] : '—');

		if( @$cell['href'] )
			$th['html'] = html_a( $cell['href'], array( 'html' => $th['html'], 'title' => isset($th['title']) ? $th['title'] : null ) );

		if( @$cell['before'] )
			$th['html'] = $cell['before'] . $th['html'];

		if( @$cell['after'] )
			$th['html'] = $th['html'] . $cell['after'];

		$header .= html_element( 'th', $th );
	}
	$header .= '</tr>';
}

if( !isset( $id ) )
	$id = 'tablelist_'.rand( 100, 999 );

$table_props = 'cellpadding="0" cellspacing="0"';
$class = 'list ' . (isset($class)?$class:'');
foreach( array( 'id', 'class', 'width' ) as $k )
	if( isset( $$k ) )
		$table_props .= sprintf( ' %s="%s"', $k, html_escape( $$k ) );

?>
<table <?php echo $table_props; ?>>
<?php if( $headtype & 1 || isset($sky) ): ?>
<thead>
	<?php if( isset($sky) ): ?>
	<tr class="tablesky"><th colspan="<?php echo $_columns; ?>"><?php echo $sky; ?></th></tr>
	<?php endif; ?>
	<?php if( $headtype & 1 ) echo $header; ?>
</thead>
<?php endif; ?>
<tbody>
<?php
foreach( $from as $rowkey => $row ):
$rowid = $id.'_'.$rowkey;
$rowclass = is_callable($row_class_callback) ? call_user_func($row_class_callback,$row,array()) : '';
?>
<tr class="<?=html_cycle("odd,even")?> <?=$rowclass?>" id="<?=$rowid?>">
	<?php $i = 0; foreach( $row as $k => $v ): $cellid = $rowid.'_'.$k; ?>
		<?php if( $i < $_columns ): ?><td id="<?=$cellid?>" class="field-<?=$k?>"><?=$v?></td><?php endif; ?>
	<?php $i++; endforeach; ?>
	<?php for( ; $i<$_columns; $i++ ) echo '<td>'.$spacer.'</td>'; ?>
</tr>
<?php
endforeach;
?>
</tbody>
<?php if( $headtype & 2 ) echo '<tfoot>'.$header.'</tfoot>'; ?>
</table>
